<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\DiyPage;
use app\model\Coupon;
use app\model\CouponReceive;
use app\model\Config;
use app\model\Goods;
use app\model\TuanGoods;
use app\model\MiaoshaGoods;
use app\model\Technical;
use app\model\DiyPageLink;

class DiypageController extends Base
{

    public function indexv2()
    {

        $id = input('get.id');

        $where['weid'] = weid();
        $where['version'] = '2';
        $Configdata = Config::getconfig();

        if (input('get.v') == $Configdata['program_version']) {
            $DiyPage = DiyPage::where($where)->order('is_audit desc')->find();
        }

        if (empty($DiyPage)) {
            if (!empty($id)) {
                $where['id'] = $id;
            }
            $DiyPage = DiyPage::where($where)->order('is_index desc')->find();
        }

        if (!empty($DiyPage)) {
            $DiyPage = $DiyPage->toArray();
            $uid = UID();
            $data['pagebase'] = iunserializer($DiyPage['pagebase'])[0];
            $data['modulelist'] = iunserializer($DiyPage['modulelist']);

            if ($data['pagebase']['base']['bg'] == 1) {
                $data['pagebase']['base']['bgstyle'] = 'background:#f7f7f7;';
            } elseif ($data['pagebase']['base']['bg'] == 2) {
                $data['pagebase']['base']['bgstyle'] = 'background:none;';
            } elseif ($data['pagebase']['base']['bg'] == 3) {
                if ($data['pagebase']['base']['bt'] == 1) {
                    $data['pagebase']['base']['bgstyle'] = 'background:' . $data['pagebase']['base']['bc'] . ';';
                } else if ($data['pagebase']['base']['bt'] == 2 && $data['pagebase']['base']['bi'] != '') {
                    $data['pagebase']['base']['bgstyle'] = 'background:url(' . $data['pagebase']['base']['bi'] . ') no-repeat 0px 0px;background-size:100%;';
                }
            }

            foreach ($data['modulelist'] as &$vo) {

                if ($vo['base']['bg'] == 1) {
                    $vo['base']['bgstyle'] = 'background:' . $vo['base']['bc'] . ';';
                } elseif ($vo['base']['bg'] == 2) {
                    $vo['base']['bgstyle'] = 'background:none;';
                } elseif ($vo['base']['bg'] == 3) {
                    if ($vo['base']['bt'] == 1) {
                        $vo['base']['bgstyle'] = 'background:' . $vo['base']['bc'] . ';';
                    } else if ($vo['base']['bt'] == 2 && $vo['base']['bi'] != '') {
                        $vo['base']['bgstyle'] = 'background:url(' . $vo['base']['bi'] . ') no-repeat 0px 0px;background-size:100%;';
                    }
                }

                if ($vo['type'] == 'window' && $vo['style']['layout'] == '-1') {
                    $ii = 0;
                    foreach ($vo['list'] as $vvo) {
                        $windowdata[$ii] = $vvo;
                        $ii++;
                    }
                    $vo['list'] = $windowdata;
                }

                if ($vo['type'] == 'goods') {
                    
                    if ($vo['base']['column'] == 2){
                        if($Configdata['listimgproportion']==43){
                            $vo['base']['widthheight'] = 'height: 253rpx;';
                        }else{
                            $vo['base']['widthheight'] = 'height: 337rpx;';
                        }
                    }
                    if ($vo['base']['column'] == 3){
                        if($Configdata['listimgproportion']==43){
                            $vo['base']['widthheight'] = 'height: 165rpx;';
                        }else{
                            $vo['base']['widthheight'] = 'height: 220rpx;';
                        }
                    }
                    if ($vo['base']['source'] == 'auto') {
                        $vo['list'] = Goods::getGoodsBycat($vo['base']['auto']['category'], $vo['base']['auto']['showNum'], $vo['base']['auto']['goodsSort']);
                    } else {
                        foreach ($vo['list'] as &$vvo1) {
                            $vvo1 = Goods::where(['weid' => weid()])->where('id', $vvo1['cm']['id'])->select()->toArray()[0];
                        }
                    }

                    $vo['list'] = Goods::setGoodslist($vo['list']);
                }

                if ($vo['type'] == 'tuan') {
                    
                    if ($vo['base']['column'] == 2){
                        if($Configdata['listimgproportion']==43){
                            $vo['base']['widthheight'] = 'height: 253rpx;';
                        }else{
                            $vo['base']['widthheight'] = 'height: 337rpx;';
                        }
                    }
                    if ($vo['base']['column'] == 3){
                        if($Configdata['listimgproportion']==43){
                            $vo['base']['widthheight'] = 'height: 165rpx;';
                        }else{
                            $vo['base']['widthheight'] = 'height: 220rpx;';
                        }
                    }
                    if ($vo['base']['source'] == 'auto') {
                        $vo['list'] = TuanGoods::where(['weid' => weid()])->limit($vo['base']['auto']['showNum'])->select()->toArray();
                    } else {
                        foreach ($vo['list'] as &$vvo1) {
                            $vvo1 = TuanGoods::where(['weid' => weid()])->where('id', $vvo1['cm']['id'])->select()->toArray()[0];
                        }
                    }

                    $vo['list'] = TuanGoods::setGoodslist($vo['list']);
                }

                if ($vo['type'] == 'miaosha') {
                    
                    if ($vo['base']['column'] == 2){
                        if($Configdata['listimgproportion']==43){
                            $vo['base']['widthheight'] = 'height: 253rpx;';
                        }else{
                            $vo['base']['widthheight'] = 'height: 337rpx;';
                        }
                    }
                    if ($vo['base']['column'] == 3){
                        if($Configdata['listimgproportion']==43){
                            $vo['base']['widthheight'] = 'height: 165rpx;';
                        }else{
                            $vo['base']['widthheight'] = 'height: 220rpx;';
                        }
                    }
                    if ($vo['base']['source'] == 'auto') {
                        $vo['list'] = MiaoshaGoods::where(['weid' => weid()])->limit($vo['base']['auto']['showNum'])->select()->toArray();
                    } else {
                        foreach ($vo['list'] as &$vvo1) {
                            $vvo1 = MiaoshaGoods::where(['weid' => weid()])->where('id', $vvo1['cm']['id'])->select()->toArray()[0];
                        }
                    }

                    $vo['list'] = MiaoshaGoods::setGoodslist($vo['list']);
                }

                if ($vo['type'] == 'technical') {
                    if ($vo['base']['column'] == 2){
                        if($Configdata['techlistimgproportion']==43){
                            $vo['base']['widthheight'] = 'height: 253rpx;';
                        }else{
                            $vo['base']['widthheight'] = 'height: 337rpx;';
                        }
                    }
                    if ($vo['base']['column'] == 3){
                        if($Configdata['techlistimgproportion']==43){
                            $vo['base']['widthheight'] = 'height: 165rpx;';
                        }else{
                            $vo['base']['widthheight'] = 'height: 220rpx;';
                        }
                    }
                    if ($vo['base']['source'] == 'auto') {
                        $vo['list'] = Technical::getdiy_bycat($vo['base']['auto']['category'], $vo['base']['auto']['showNum'], $vo['base']['auto']['goodsSort']);
                    }else{
                        foreach ($vo['list'] as &$vvo1) {
                            $vvo1 = Technical::where(['weid' => weid()])->where('id', $vvo1['cm']['id'])->select()->toArray()[0];
                        }
                    }
                }

                if ($vo['type'] == 'search') {
                    if (!empty($vo['params']['hotkey'])) {
                        $vo['params']['hotkey'] = explode(" ", $vo['params']['hotkey']);
                    }

                    if ($data['items']['page']['style']['titleBackgroundColor'] == '#ffffff') {
                        $vo['style']['background'] = '#f7f7f7';
                        $vo['style']['hotkeytxtColor'] = '#666666';
                    } else {
                        $vo['style']['background'] = $data['items']['page']['style']['titleBackgroundColor'];
                        $vo['style']['hotkeytxtColor'] = '#ffffff';
                    }
                }

                if ($vo['type'] == 'coupon') {

                    $coupondata = Coupon::where(['weid' => weid(), 'ptype' => 1])
                        ->limit($vo['max'])
                        ->select()
                        ->toArray();

                    foreach ($coupondata as $cvo) {
                        $cdata[$cvo['id']]['coupon_id'] = $cvo['id'];
                        $cdata[$cvo['id']]['color'] = $cvo['color'];
                        $cdata[$cvo['id']]['coupon_type'] = $cvo['coupon_type'];
                        $cdata[$cvo['id']]['discount'] = $cvo['discount'];
                        $cdata[$cvo['id']]['reduce_price'] = $cvo['reduce_price'];
                        $cdata[$cvo['id']]['min_price'] = $cvo['min_price'];
                        $cdata[$cvo['id']]['state'] = CouponReceive::getReceiveState($cvo['id'], $uid);
                    }
                    $vo['list'] = $cdata;
                }

                if (!empty($vo['list'])) {
                    foreach ($vo['list'] as &$vvo1) {
                        if (!empty($vvo1['link'])) {
                            $vvo1['link'] = DiyPageLink::setlink($vvo1['link']);
                        }
                    }
                }

                if (!empty($vo['link'])) {
                    $vo['link'] = DiyPageLink::setlink($vo['link']);
                }
            }
        }

        
        $Configdata['SITE_ICON'] = toimg($data['SITE_ICON']);
        $Configdata['poster'] = toimg($data['poster']);
        $Configdata['applypic'] = toimg($data['applypic']);
        $data['config'] =  $Configdata;
        return $this->json(['data' => $data]);
    }
}
